<template>
    <div>
        项目名称 : <input type="text" v-model="cond.name">&ensp;
        <input type="button" value="查询" @click="getdata">
        <div style="float: right;">
            <input type="button" value="批量启用" @click="batchup">&ensp;
            <input type="button" value="批量禁用" @click="batchdown">&ensp;
            <input type="button" value="导出" @click="exportfile">
        </div>
        <table class="table table-hover">
            <thead>
                <tr>
                    <input type="checkbox" v-model="allcheck" @change="checked">
                    <td>编号</td>
                    <td>项目编码</td>
                    <td>康复理疗项目名称</td>
                    <td>服务人数</td>
                    <td>服务时长(小时)</td>
                    <td>佣金(元)</td>
                    <td>项目状态</td>
                    <td>操作</td>
                </tr>
            </thead>
            <tbody>
                <tr v-for="value in healthy.bodys" :key="value.bid">
                    <input type="checkbox" v-model="check" :value="value.bid">
                    <td>{{ value.bid }}</td>
                    <td>{{ value.bcode }}</td>
                    <td>{{ value.bname }}</td>
                    <td>{{ value.bpeople }}</td>
                    <td>{{ value.bhour }}</td>
                    <td>{{ value.bmoney.toFixed(2) }}</td>
                    <td>{{ value.bstate ? "启用" : "禁用" }}</td>
                    <td>
                        <input type="button" value="删除" @click="dele(value.bid)">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <span>共{{ pageslip }}条</span>&ensp;
    <span>每页{{ cond.pagesize }}条</span>&ensp;
    <span>共{{ cond.page }}/{{ pagecount }}页</span>&ensp;&ensp;&ensp;
    <input type="button" value="首页" @click="pagee(1)">
    <input type="button" value="上一页" @click="pagee(cond.page-1)">
    <input type="button" v-for="value in pagecount" :value="value" @click="pagee(value)">
    <input type="button" value="下一页" @click="pagee(cond.page+1)">
    <input type="button" value="尾页" @click="pagee(pagecount)">
</template>

<script setup lang="ts">
    onMounted(()=>
    {
        getdata()
    })
    import axios from 'axios';
    import { onMounted, ref } from 'vue';
    let pagecount: any = ref({});
    let pageslip = ref({});
    let allcheck=ref(false);
    let check=ref([{}]);
    let healthy = ref
    ({
        "count": 0,
        "bodys": 
        [
            {
                "bid": 0,
                "bcode": "string",
                "bname": "string",
                "bpeople": 0,
                "bhour": 0,
                "bmoney": 0,
                "bdel": true,
                "bstate": true
            }
        ]
    })
    let cond = ref
    ({
        page:1,
        pagesize:2,
        name:null
    })
    const exportfile = () =>
    {
        location.href ="https://localhost:7080/api/BodyHealthy/ExportFile"
    }
    const batchup = () =>
    {
        if (check.value.length == 0)
        {
            alert('至少要选择一项哦')
            return;
        }
        if(!confirm("确认批量更改所选数据的状态值嘛?"))
        {
            return;
        }
        axios
        ({
            url: "https://localhost:7080/api/BodyHealthy/BatchupState",
            method: "get",
            params: { ids: check.value.toString() }
        })
        .then((res)=>
        {
            console.log(res);
            if(res.data>0)
            {
                alert("状态更改成功")
                allcheck.value=false;
                check.value=[];
                getdata();
            }
            else
            {
                alert("状态更改失败")
            }
        })
    }
    const batchdown = () => 
    {
        if (check.value.length == 0) 
        {
            alert('至少要选择一项哦')
            return;
        }
        if (!confirm("确认批量更改所选数据的状态值嘛?")) 
        {
            return;
        }
        axios
        ({
            url: "https://localhost:7080/api/BodyHealthy/BatchdownState",
            method: "get",
            params: { ids: check.value.toString() }
        })
        .then((res) => 
        {
            console.log(res);
            if (res.data > 0) 
            {
                alert("状态更改成功")
                allcheck.value = false;
                check.value = [];
                getdata();
            }
            else 
            {
                alert("状态更改失败")
            }
        })
    }
    const checked = () =>
    {
        if(allcheck.value==true)
        {
            check.value=healthy.value.bodys.map((item: any)=>
            {
                return item.bid;
            })
        }
        else
        {
            check.value=[];
        }
    }
    const dele = (id: any) =>
    {
        if(!confirm("确认删除吗?"))
        {
            return;
        }
        axios
        ({
            url:"https://localhost:7080/api/BodyHealthy/DeleteHealthy?id="+id,
            method:"get"
        })
        .then((res)=>
        {
            console.log(res);
            if (res.data > 0) 
            {
                alert("康复理疗信息删除成功")
                getdata()
            }
            else 
            {
                alert("康复理疗信息删除失败")
            }
        })
    } 
    const pagee = (num: any) =>
    {
        if(num<1)
        {
            cond.value.page=1;
            return
        }
        if(num>pagecount)
        {
            cond.value.page=pagecount
            return;
        }
        cond.value.page=num;
        getdata()
    }
    const getdata = () =>
    {
        axios
        ({
            url:"https://localhost:7080/api/BodyHealthy/GetHealthys",
            method:"get",
            params:cond.value
        })
        .then((res)=>
        {
            console.log(res);
            healthy.value=res.data
            pagecount=Math.ceil(res.data.count/cond.value.pagesize);
            pageslip=res.data.count;
        })
        .catch((err)=>
        {
            console.log(err);
        })
    }
</script>

<style lang="scss" scoped>

</style>